local skel = fk.CreateSkill {
  name = "lb__qianshen",
}

Fk:loadTranslationTable {
  ["lb__qianshen"] = "遣神",
  --[":lb__qianshen"] = "当你的判定牌生效后，你可以使用之，此牌结算后，若造成了伤害，你可以令一名角色回复1点体力。 ",
  --9.24
  [":lb__qianshen"] = "当你的判定牌生效后，你可以使用之，此牌结算后，若你本回合首次因此造成伤害，你可以令一名角色回复1点体力。",


  ["#lb__qianshen_hp-ask"] = "遣神：你可以令一名角色回复1点体力",

  ["$lb__qianshen1"] = "灵符…保命…",
  ["$lb__qianshen2"] = "驱邪…缚魅…",
}

skel:addEffect(fk.FinishJudge, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and data.who == player and target == player
  end,
  on_cost = function(self, event, target, player, data)
    local use = player.room:askToUseRealCard(player, {
      pattern = { data.card.id },
      skill_name = skel.name,
      extra_data = { expand_pile = { data.card.id } },
      skip = true,
    })
    event:setCostData(self, { use = use })
    return use
  end,
  on_use = function(self, event, target, player, data)
    local use = event:getCostData(self).use ---@cast use UseCardDataSpec
    use.extra_data = use.extra_data or {}
    use.extra_data.lb__qianshen = true
    player.room:useCard(use)
  end,
})

skel:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    local parent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true) --父事件
    return player:hasSkill(skel.name) and target == player
        and parent and parent.data and parent.data.extra_data and parent.data.extra_data.lb__qianshen
        and #player.room.logic:getActualDamageEvents(1, function(e)
          local damage = e.data
          return damage.card and damage.card.id == data.card.id and not damage.prevented
        end, nil, parent.id) > 0
        and player:getMark("lb__qianshen-turn") == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local ps = table.filter(room:getAlivePlayers(), function(p)
      return p:isWounded()
    end)
    room:setPlayerMark(player, "lb__qianshen-turn", 1)
    if #ps > 0 then
      local tos = room:askToChoosePlayers(player, {
        targets = ps,
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        prompt = "#lb__qianshen_hp-ask",
      })
      event:setCostData(self, { tos = tos })
      return #tos > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:recover({
      who = event:getCostData(self).tos[1],
      num = 1,
      recoverBy = player,
      skillName = skel.name,
    })
  end,
})

return skel
